package de.fhd.pms.ctrl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import de.fhd.pms.dao.AutoDao;
import de.fhd.pms.dao.AutoDeleteException;
import de.fhd.pms.dao.KundeDao;
import de.fhd.pms.dao.StationDao;
import de.fhd.pms.model.Auto;
import de.fhd.pms.model.Kunde;
import de.fhd.pms.model.Station;

/**
 * The Controller class for {@link Auto} objects. 
 * Its tasks:
 * <ul>
 * <li>read and verify the parameters</li>
 * <li>access to the objects of the system</li>
 * <li>selection of the view and provision of content for the view</li>
 * </ul>
 * 
 * @author Fischer, Neetzel & Six
 *
 */
@Controller
public class AutoController {
	private static Logger log = Logger.getLogger(AutoController.class);
	private AutoDao autoDao = null;
	private StationDao stationDao = null;
	private KundeDao kundeDao = null;

	public void setAutoDao(AutoDao autoDao) {
		this.autoDao = autoDao;
	}

	public void setStationDao(StationDao stationDao) {
		this.stationDao = stationDao;
	}

	public void setKundeDao(KundeDao kundeDao) {
		this.kundeDao = kundeDao;
	}

	/**
	 * <p>
	 * Auto form request.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/SetAuto.html'.
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/SetAuto.html", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam(required = false) Integer id) {
		log.debug("EditAuto.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Auto());
		} else {
			mv.addObject(autoDao.findById(id));
		}
//		mv.addObject(stationDao.findById(id));
		mv.addObject("station", stationDao.findAll());
		mv.setViewName("admin-set-auto");
		return mv;
	}
	
//	ORIGINAL
//    @RequestMapping(value="/SetAuto.html", method=RequestMethod.GET)
//    public ModelAndView edit(@RequestParam(required=false) Integer id) {
//    	log.debug("EditAuto.html");
//    	ModelAndView mv = new ModelAndView();
//    	if (id == null) {
//    		mv.addObject(new Auto());
//    	} else { 
//    		mv.addObject(autoDao.findById(id));
//    	}
//    	mv.setViewName("set-auto");
//    	return mv;
//    }

	/**
	 * <p>
	 * Saves a Auto.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/SaveAuto.html'.
	 * </p>
	 * 
	 * @return
	 */

//	ORIGINAL
//	@RequestMapping(value = "/SaveAuto.html", method = RequestMethod.POST)
//	public String save(Auto auto, Model model) {
//		if (auto.getCreated() == null) {
//			auto.setCreated(new Date());
//		}
//		Auto result = autoDao.save(auto);
//		// set id from create
//		if (auto.getId() == null) {
//			auto.setId(result.getId());
//		}
//		model.addAttribute("statusMessageKey", "kunde.form.msg.success");
//		return "redirect:AlleAutos.html";
//	}
	
//	VERSUCH
	@RequestMapping(value = "/SaveAuto.html", method = RequestMethod.POST)
	public String save(Auto auto, int stationId, Model model) {
		log.debug("SaveAuto.html autoId=" + auto.getId() + " stationId=" + stationId);
		if (auto.getCreated() == null) {
			auto.setCreated(new Date());
		}
//		Auto result = autoDao.save(auto, stationId);
		// set id from create
//		if (auto.getId() == null) {
//			auto.setId(result.getId());
//		}
		Station station = stationDao.findById(stationId);
		auto.setStation(station);
		autoDao.save(auto);
		model.addAttribute("statusMessageKey", "kunde.form.msg.success");
		return "redirect:AlleAutos.html";
	}
	
	/**
	 * <p>
	 * Deletes a Auto.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/LoescheAuto.html'.
	 * </p>
	 */
//	EXCEPTION VERSUCH
//	@RequestMapping(value = "/LoescheAuto.html", method = RequestMethod.GET)
//	public String delete(Auto auto) {
//		try {
//			autoDao.delete(auto);
//		} catch (AutoDeleteException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return "redirect:AlleAutos.html";
//	}
	
//	ORIGINAL
    @RequestMapping(value="/LoescheAuto.html", method=RequestMethod.GET)
    public String delete(Auto auto) {
        autoDao.delete(auto);
        return "redirect:AlleAutos.html";
    }

	/**
	 * <p>
	 * Searches for all autos and returns them in a <code>Collection</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/AlleAutos.html'.
	 * </p>
	 */
	@RequestMapping(value = "/AlleAutos.html", method = RequestMethod.GET)
	public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Auto bearbeiten");
		mv.addObject("message", "Alle Autos");
		// Mit der Methode findAll() werden alle Auto-Objekte aus der DB geholt
		// -> DB-Zugriff
		mv.addObject("autos", autoDao.findAll());
		mv.addObject("edit", true);
		mv.setViewName("admin-list-autos");
		return mv;
	}

	/**
	 * <p>
	 * Searches for all free autos and returns them in a <code>Collection</code>
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/FreieAutos.html'.
	 * </p>
	 */

	@RequestMapping(value = "/KundenAutos.html", method = RequestMethod.GET)
	public ModelAndView freieAutos() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Fahrt beginnen");
		mv.addObject("message", "Hier sehen Sie alle zurzeit verfügbaren Fahrzeuge, die auf einer Station geparkt wurden. Wählen Sie ihr Wunschfahrzeug für eine Fahrt aus.");
		mv.addObject("autos", autoDao.findAllDTO());
		mv.addObject("edit", false);
		mv.setViewName("kunde-list-autos");
		return mv;
	}
	
//	ORIGINAL
//    @RequestMapping(value="/KundenAutos.html", method=RequestMethod.GET)
//    public ModelAndView freieAutos() {
//		ModelAndView mv = new ModelAndView();
//		mv.addObject("title", "Fahrt beginnen");
//		mv.addObject("message", "Folgende Autos sind für eine Fahrt verfügbar. Wählen Sie ein Auto aus:");
//		mv.addObject("autos", autoDao.findFreie());
//		mv.addObject("edit", false);
//		mv.setViewName("kunden-list-autos");
//		return mv;
//    }

}
